package com.chengzhi.filter;

import com.alibaba.fastjson.JSON;
import com.chengzhi.entity.ZuulResponse;
import com.google.gson.JsonObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author ruanchengzhi
 * @description: TODO
 * @date 2019/9/28 14:39
 */
@Component
public class SecondFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 2;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String parameter = request.getParameter("a");
        if (Objects.isNull(parameter)) {
            context.setSendZuulResponse(false);
            ZuulResponse zuulResponse = new ZuulResponse();
            zuulResponse.setCode(500);
            zuulResponse.setMessage("a参数为空");
            context.setResponseBody(JSON.toJSONString(zuulResponse));
            context.getResponse().setContentType("text/html;charset=UTF-8");
            context.set("logic-is-success", false);
            return null;
        }
        context.set("logic-is-success", true);
        return null;
    }
}
